<script>
	import {
		getPermissions
	} from '@/config/API/user.js';
	import {
		shareLogin
	} from './././config/API/login'
	import {
		wxLogin
	} from './config/request/wxLogin';
	export default {
		onLaunch: async function() {
			try {
				const localUserInfo = uni.getStorageSync('userInfo');
				let PermissionsID = localUserInfo ? localUserInfo.identify : null;
				const userID = uni.getStorageSync('userInfo').id
				if (userID) {
					console.log('localUserInfo.identify', PermissionsID);
					// 调用后端接口获取最新的 identify 值
					const response = await getPermissions(userID);
					const serverIdentify = response.data;

					// 检查 identify 是否有变化
					if (localUserInfo.identify !== serverIdentify) {
						console.log('检测到用户权限变更，正在同步...');

						// 更新 identify 字段
						const updatedUserInfo = {
							...localUserInfo,
							identify: serverIdentify,
						};

						// 存储更新后的 userInfo 到本地
						uni.setStorageSync('userInfo', updatedUserInfo);
						this.globalData.userInfo = updatedUserInfo; // 更新全局数据
						console.log('存储更新后的 userInfo 到本地', updatedUserInfo);
					}
				} else {
					console.log('用户权限没变化或者没有登录');
				}
			} catch (error) {
				console.error('同步用户信息失败', error);
			}

			console.log('App Launch');
		},
		onShow: async function() {
			let launchOptions = wx.getLaunchOptionsSync();
			console.log('启动小程序的路径:', launchOptions.path);
			console.log('启动小程序的场景值:', launchOptions.scene);
			console.log('启动小程序的 query 参数:', launchOptions.query);
			//判断所携带参数是否存在
			const code = await wxLogin.api()

			if (launchOptions.query.id&&!uni.getStorageSync('userInfo')) {
				console.log('分享code=》》》', code, launchOptions.query.id)
				const item = {
					code: code,
					position: "成都市",
					friend_id: launchOptions.query.id
				}
				const data = await shareLogin(item)
				console.log('分享结果', data)
				// uni.showToast({
				// 	title: '被邀请成功!',
				// 	icon: "success"
				// });
				console.log('分享返利')
			}
		},
		onLoad: function(options) {
			console.log('参数', options)
		},
		onHide: function() {
			console.log('App Hide');
		},
		onUnload: function() {
			uni.removeStorageSync('activeCategory');
			console.log('onUnload');
		},
	};
</script>


<style>
	/deep/ ::-webkit-scrollbar {
		display: none;
		width: 0 !important;
		height: 0 !important;
		-webkit-appearance: auto !important;
		background: transparent;
		overflow: auto !important;
	}
	/* 在线链接服务仅供平台体验和调试使用，平台不承诺服务的稳定性，企业客户需下载字体包自行发布使用并做好备份。 */
</style>